import { emrStatus } from '@/hooks/event/useEmr.js'
const { giveUpEditing } = emrStatus()
import { debounce } from 'lodash'
import store from '@/store'
// 是否重复跳转

export function emrPermissionGuard(router) {
  let jumpSign = 0
  // 该路由守卫 需要el-menu 设置 :router="true"
  router.beforeEach((to, from, next) => {
    const urls = ['/workbench/reception/medicalRecord', '/shop/editShops']
    const skipEmrPermissionGuard = to.query.skipEmrPermissionGuard
    if (urls.includes(to.path) && urls.includes(from.path)) {
      next()
    } else if (urls.includes(from.path) && !skipEmrPermissionGuard) {
      const back = debounce(
        () => {
          giveUpEditing()
            .then(() => {
              jumpSign = 0
              next()
            })
            .catch((err) => {
              jumpSign = 0
              next(false)
            })
        },
        500,
        { leading: true, trailing: false }
      )
      back()
      // // 如果
      // jumpSign += 1
      // if (jumpSign < 2) return
      // giveUpEditing()
      //   .then(() => {
      //     jumpSign = 0
      //     next()
      //   })
      //   .catch((err) => {
      //     jumpSign = 0
      //     next(false)
      //   })
    } else {
      next()
      // return
    }
  })

  function resetSign(pointer) {
    setTimeout(() => {
      jumpSign = false
    }, 1000)
  }
}
